package io.renren.controller;

import com.alibaba.fastjson.JSONArray;
import io.renren.entity.SysOfficeEntity;
import io.renren.entity.SysUserEntity;
import io.renren.entity.VO.SysOfficeEntityVO;
import io.renren.service.SysOfficeService;
import io.renren.utils.PageUtils;
import io.renren.utils.Query;
import io.renren.utils.R;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


/**
 * 机构表
 * 
 * @author zhangheng
 * @date 2017-06-12 11:44:54
 */
@RestController
@RequestMapping("sysoffice")
public class SysOfficeController {
	@Autowired
	private SysOfficeService sysOfficeService;
	
	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("sysoffice:list")
	public R list(@RequestParam Map<String, Object> params){
//		String limit = request.getParameter("limit");
//		String page = request.getParameter("page");
//		params.put("page",page);
//		params.put("limit",limit);
		//查询列表数据
        Query query = new Query(params);

		List<SysOfficeEntityVO> sysOfficeList = sysOfficeService.queryList(query);//sysOfficeService.getTree();
		int total = sysOfficeService.queryTotal(query);
		
		PageUtils pageUtil = new PageUtils(sysOfficeList, total, query.getLimit(), query.getPage());
		
		return R.ok().put("page", pageUtil);
	}


	/**
	 * 信息
	 */
	@RequestMapping("/info/{id}")
	@RequiresPermissions("sysoffice:info")
	public R info(@PathVariable("id") String id){
		SysOfficeEntity sysOffice = sysOfficeService.queryObject(id);
		
		return R.ok().put("sysOffice", sysOffice);
	}
	
	/**
	 * 保存
	 */
	@RequestMapping("/save")
	@RequiresPermissions("sysoffice:save")
	public R save(@RequestBody SysOfficeEntity sysOffice){
		sysOfficeService.save(sysOffice);
		return R.ok();
	}
	
	/**
	 * 修改
	 */
	@RequestMapping("/update")
	@RequiresPermissions("sysoffice:update")
	public R update(@RequestBody SysOfficeEntity sysOffice){
		sysOfficeService.update(sysOffice);
		
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	@RequiresPermissions("sysoffice:delete")
	public R delete(@RequestBody String id){
		String[] ids = new String[]{id};
		sysOfficeService.deleteBatch(ids);
		
		return R.ok();
	}

	/**
	 * 树菜单
	 */
	@RequestMapping("/tree")
	@RequiresPermissions("sysoffice:list")
	public R getTree(){
		SysOfficeEntityVO sys = sysOfficeService.getTree();
		JSONArray jsonArray = new JSONArray();
		jsonArray.add(sys);
		return R.ok().put("list",jsonArray);
	}

	@RequestMapping("/getlistbydeptno")
	@RequiresPermissions("sysoffice:list")
	public R getDeptno(@RequestParam Map<String, Object> params){
		Query query = new Query(params);
		List<SysUserEntity> list = sysOfficeService.getDeptno(params.get("id").toString());
		PageUtils pageUtil = new PageUtils(list, list.size(), query.getLimit(), query.getPage());

		return R.ok().put("page", pageUtil);
	}




	/**
	 * 列表
	 */
	@RequestMapping("/selectDept")
	public R selectDept(@RequestParam Map<String, Object> params){
//		//查询列表数据
		Query query = new Query(params);
		List<SysOfficeEntity> accidentsManagementList = sysOfficeService.queryDeptList(query);
		int total = sysOfficeService.queryTotal(query);
		PageUtils pageUtil = new PageUtils(accidentsManagementList, total, query.getLimit(), query.getPage());

		return R.ok().put("page", pageUtil);
	}

}
